<template>
  <!-- 地图管理组件 -->
  <div class="job">
    <!-- 面包屑路径导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>职业管理</el-breadcrumb-item>
      <el-breadcrumb-item>职业列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容区 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <div class="grid-content search-container">
            <el-input
              placeholder="请输入内容"
              v-model.trim="searchCont"
              clearable
              class="search-input"
              @clear="searchClear"
            >
            </el-input>
            <el-button
              type="info"
              plain
              icon="el-icon-search"
              @click="getJobList"
            ></el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 地图列表内容 -->
      <div class="job-table">
        <el-table :data="jobList" border style="width: 100%">
          <el-table-column prop="id" label="职业id"></el-table-column>
          <el-table-column prop="job_id" label="职业编号"></el-table-column>
          <el-table-column prop="job_name" label="职业名称"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="props">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editJob(props.row.id)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteJob(props.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageConfig.currentPage"
          :page-sizes="pageConfig.pageSizes"
          :page-size="pageConfig.pageSize"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageConfig.pageTotal"
          :hide-on-single-page="false"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 修改职业弹出层 -->
    <el-dialog
      title="修改职业"
      :visible.sync="dialogConfig.dialogEditJobVisible"
      class="edit-job-dialog"
    >
      <el-form
        :model="editJobForm"
        :rules="editJobFormRules"
        ref="editJobForm"
        class="demo-ruleForm"
        :label-position="'right'"
        label-width="80px"
      >
        <el-form-item label="职业编号">
          <el-input v-model.trim="editJobForm.jobId" disabled></el-input>
        </el-form-item>
        <el-form-item label="职业名称" prop="jobName">
          <el-input v-model.trim="editJobForm.jobName"></el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button @click="dialogConfig.dialogEditJobVisible = false"
            >取消</el-button
          >
          <el-button type="primary" @click="editJobSubmit('editJobForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索内容
      searchCont: '',
      // 地图列表数据
      jobList: [],
      // 资源路径
      path: '',
      // 分页器参数配置
      pageConfig: {
        pageTotal: 0, // 分页器总条数
        pageSize: 10, // 每页显示条数
        pageSizes: [5, 10, 15, 20], // 可选择的每页条数
        currentPage: 1 // 分页器当前页码
      },
      // 弹出层配置
      dialogConfig: {
        dialogEditJobVisible: false
      },
      // 加载层参数配置
      loadingConfig: {
        target: 'body',
        fullscreen: true,
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.1)',
        text: '拼命加载中'
      },
      editJobForm: {
        id: null,
        jobId: '',
        jobName: ''
      },
      editJobFormRules: {
        jobName: [
          { required: true, message: '请输入职业名称', trigger: 'blur' },
          {
            min: 1,
            max: 5,
            message: '职业名称在 1 到 5 个字符',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    getJobList () {
      // 获取地图列表
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      console.log(this);
      // var loading = new this.Loading();
      this.$axios
        .get('/jobList', {
          params: {
            search: this.searchCont.trim(),
            pagenum: this.pageConfig.currentPage,
            pagesize: this.pageConfig.pageSize
          }
        })
        .then(res => {
          if (res.status === 0) {
            console.log(res);
            // 处理数据
            res.data.forEach(item => {
              if (item.map_status === 1) {
                item.map_status = true;
              } else {
                item.map_status = false;
              }
            });
            // 设置分页
            this.pageConfig.pageTotal = res.total;
            this.pageConfig.currentPage = res.pagenum;
            // 设置数据
            this.jobList = res.data;
            this.path = res.path;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    searchClear () {
      this.getJobList();
    },
    editJob (id) {
      // 根据地图ID查找信息
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      // 发起请求
      this.$axios
        .get('/job/' + id)
        .then(res => {
          if (res.status === 0) {
            // 设置数据
            this.editJobForm.id = res.data.id;
            this.editJobForm.jobId = res.data.job_id;
            this.editJobForm.jobName = res.data.job_name;
            // 打开弹出层
            this.dialogConfig.dialogEditJobVisible = true;
          } else {
            // 提示失败信息
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    editJobSubmit (formName) {
      // 修改职业提交
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 打开加载层
          var loading = this.$loading.service(this.loadingConfig);
          this.$axios
            .put('/job/' + this.editJobForm.id, {
              jobName: this.editJobForm.jobName
            })
            .then(res => {
              if (res.status === 0) {
                // 关闭弹出层
                this.dialogConfig.dialogEditJobVisible = false;
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getJobList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        }
      });
    },
    handleSizeChange (val) {
      // 切换每页条数重新获取数据
      this.pageConfig.pageSize = val;
      this.getJobList();
    },
    handleCurrentChange (val) {
      // 切换页码时重新获取数据
      this.pageConfig.currentPage = val;
      this.getJobList();
    }
  },
  mounted () {
    this.getJobList();
  }
};
</script>

<style lang="less" scoped></style>
